package org.easyway.objects.sprites2D.sentry;

import java.util.ArrayList;

import org.easyway.interfaces.base.IPureRender;
import org.easyway.interfaces.extended.IDrawing;
import org.easyway.interfaces.extended.ILayerID;
import org.easyway.interfaces.sprites.ICollisionable;
import org.easyway.interfaces.sprites.IMaskerable;
import org.easyway.interfaces.sprites.IPlain2D;
import org.easyway.interfaces.sprites.ISpriteColl;
import org.easyway.objects.BaseObject;
import org.easyway.objects.sprites2D.Mask;
import org.easyway.objects.sprites2D.StaticFullyMask;
import org.easyway.system.StaticRef;
import org.lwjgl.opengl.GL11;

public class Sentry extends BaseObject implements IPureRender, IPlain2D,
		ICollisionable, IDrawing, IMaskerable, ILayerID, ISpriteColl {

	private static final long serialVersionUID = -3159539610220239454L;

	public static boolean showSentryes = true;
	
	/**
	 * coordinates
	 */
	protected float x, y;

	/**
	 * size
	 */
	protected int width, height;

	/**
	 * indicates if the object is added to the collisionable list
	 */
	protected boolean isAdded;

	protected ArrayList<ISpriteColl> collisionList = new ArrayList<ISpriteColl>(10);

	protected Mask mask;
	
	protected float color = 1.0f;

	SentrySprite father;

	/** Depth of sprite */
	private int layer; // private

	/**
	 * the drawing sheet
	 */
	private int idLayer = -1;

	public Sentry(SentrySprite father, String name) {
		this.father = father;
		type.set(name);

		// top
		setIdLayer(StaticRef.layers.length - 1);
		setLayer(Integer.MAX_VALUE);
	}

	public Sentry(SentrySprite father, String name, float x, float y,
			int width, int height) {
		this.father = father;
		type.set(name);
		setXY(x + father.getX(), y + father.getY());
		setSize(width, height);
		setFullMask();

		// top
		setIdLayer(StaticRef.layers.length - 1);
		setLayer(Integer.MAX_VALUE);
	}
	
	public Sentry(Sentry obj) {
		super(obj);
		x = obj.x;
		y = obj.y;
		width = obj.width;
		height = obj.height;
		isAdded = obj.isAdded; // should be false..
		mask = obj.mask;
		color = obj.color;
		father = obj.father; // we DON'T add the Sentry to the father.. :)
		setIdLayer(obj.idLayer);
		setLayer(obj.layer);
	}
	
	public void render() {
		if (showSentryes) {
			GL11.glDisable(GL11.GL_TEXTURE_2D);
			GL11.glBegin(GL11.GL_QUADS);
			GL11.glColor3f(color, 1-color, 0);
			GL11.glVertex2f(x - StaticRef.getCamera().x, y - StaticRef.getCamera().y);
			GL11.glVertex2f(x - StaticRef.getCamera().x + width, y
					- StaticRef.getCamera().y);
			GL11.glVertex2f(x - StaticRef.getCamera().x + width, y
					- StaticRef.getCamera().y + height);
			GL11.glVertex2f(x - StaticRef.getCamera().x, y - StaticRef.getCamera().y
					+ height);
			GL11.glEnd();
			GL11.glEnable(GL11.GL_TEXTURE_2D);
			color = 1.0f;
		}
	}

	public int getHeight() {
		return height;
	}

	public int getWidth() {
		return width;
	}

	/**
	 * sets the size of object
	 * 
	 * @param width
	 *            the width
	 * @param height
	 *            the height
	 */
	public void setSize(int width, int height) {
		this.width = width;
		this.height = height;
	}

	public float getX() {
		return x;
	}

	public float getY() {
		return y;
	}
	
	public float getXOnScreen() {
		return x;
	}

	public float getYOnScreen() {
		return y;
	}

	public void setXY(float x, float y) {
		this.x = x;
		this.y = y;
	}

	public boolean getAddedToCollisionList() {
		return isAdded;
	}

	public void setAddedToCollisionList(boolean value) {
		isAdded = value;
	}

	public ArrayList<ISpriteColl> getCollisionList() {
		return collisionList;
	}

	public void onCollision() {
		color = 0.0f;
		father.sentryCollision(this);
	}

	public Mask getMask() {
		return mask;
	}

	/**
	 * sets the mask
	 * 
	 * @param mask
	 *            the mask to set
	 */
	public void setMask(Mask mask) {
		this.mask = mask;
	}

	/**
	 * sets the default fully static rectangolar mask.
	 * 
	 */
	public void setFullMask() {
		mask = new StaticFullyMask(width, height);
	}

	/** sets the depth */
	public void setLayer(int layer) {
		this.layer = layer;
		readdToDrawingLists();
	}

	public int getLayer() {
		return layer;
	}

	public int getIdLayer() {
		return idLayer;
	}

	public void setIdLayer(int id) {
		if (idLayer != -1) {
			StaticRef.layers[idLayer].remove(this);
		}
		if (id < 0) {
			id = 0;
		} else if (id > StaticRef.layers.length) {
			id = StaticRef.layers.length;
		}
		idLayer = id;
		StaticRef.layers[idLayer].add(this);
	}

	public float getRotation2D() {
		return 0;
	}

	public void setHeight(int height) {
		this.height = height;
		
	}

	public void setWidth(int width) {
		this.width = width;
	}

	/* (non-Javadoc)
	 * @see org.easyway.interfaces.sprites.IPlain2D#setX(float)
	 */
	public void setX(float x) {
		this.x = x;
	}

	/* (non-Javadoc)
	 * @see org.easyway.interfaces.sprites.IPlain2D#setY(float)
	 */
	public void setY(float y) {
		this.y = y;
	}
	
	public Object clone() {
		return new Sentry(this);
	}

}
